package com.sakana.studentsystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {

    private static final String ADD_STUDENT="1";
    private static final String DELETE_STUDENT="2";
    private static final String UPDATE_STUDENT="3";
    private static final String QUERY_STUDENT="4";
    private static final String EXIT="5";

    //添加学生
    public  void startStudentSystem() {
        ArrayList<Student> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("-------------欢迎来到sakana学生管理系统--------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入你的选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case ADD_STUDENT -> addStudent(list);
                case DELETE_STUDENT -> deleteStudent(list);
                case UPDATE_STUDENT -> updateStudent(list);
                case QUERY_STUDENT -> queryStudent(list);
                case EXIT -> {
                    System.out.println("退出");
                    break loop;
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list) {

        Student student = new Student();

        Scanner sc=new Scanner(System.in);



        //判断id唯一吗
        while (true) {
            System.out.println("请输入id：");
            String id = sc.next();
            boolean flag=contains(list,id);
            if(flag){
                System.out.println("已存在id,重新输入");
            }else {
                student.setId(id);
                break;
            }
        }

        System.out.println("请输入姓名：");
        student.setName(sc.next());

        System.out.println("请输入年龄：");
        student.setAge(sc.nextInt());

        System.out.println("请输入家庭住址：");
        student.setAddress(sc.next());

        list.add(student);

        System.out.println("学生信息添加成功");

    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入删除同学的id:");
        String sid=sc.next();
        int index = getIndex(list, sid);
        if(index>=0){
            list.remove(index);
            System.out.println("id为"+sid+"的同学删除成功");
        }else {
            System.out.println("id不存在，删除失败");
        }
    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入修改同学的id:");
        String id = sc.next();

        int index = getIndex(list, id);
        if(index==-1){
            System.out.println("id为"+id+"的同学不存在，请重新输入");
            return;
        }

        Student student = list.get(index);

        System.out.println("请输入要修改的姓名：");
        String newName = sc.next();
        student.setName(newName);

        System.out.println("请输入要修改的年龄：");
        int newAge = sc.nextInt();
        student.setAge(newAge);

        System.out.println("请输入要修改的地址");
        String newAddress = sc.next();
        student.setAddress(newAddress);

    }

    //查询学生
    public static void queryStudent(ArrayList<Student> list) {
        if(list.size()==0){
            System.out.println("当前没学生信息，请添加后再查询");
            return;
        }

        System.out.println("id\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getAddress());

        }
    }

    public static boolean contains(ArrayList<Student>list,String id){
//        for (int i = 0; i < list.size(); i++) {
//            Student student = list.get(i);
//            if(student.getId().equals(id)){
//                return true;
//            }
//        }
        return getIndex(list,id)>=0;
    }

    public static int getIndex(ArrayList<Student>list,String id){
        for (int i = 0; i < list.size(); i++) {
               Student s =list.get(i);
               if(s.getId().equals(id)){
                   return i;
               }
        }
        return -1;
    }
}
